/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medicalassistant;
import java.sql.*;
import java.text.*;
/**
 *
 * @author Thomas
 */
public class AddDoctorPanel extends javax.swing.JPanel {

    /**
     * Creates new form AddDoctorPanel
     */
    public AddDoctorPanel(int ID, int userType, Connection connect) {
        initComponents();
        try {
            Class.forName("org.sqlite.JDBC");
            conn = connect;
            stat = conn.createStatement();
        } catch(ClassNotFoundException ex) {System.out.println(ex.getMessage());}
        catch(SQLException ex) {System.out.println(ex.getMessage());}
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        zip = new javax.swing.JTextField();
        zipLabel = new javax.swing.JLabel();
        state = new javax.swing.JTextField();
        stateLabel = new javax.swing.JLabel();
        city = new javax.swing.JTextField();
        firstNameLabel = new javax.swing.JLabel();
        addDoctorHeader = new javax.swing.JLabel();
        password = new javax.swing.JPasswordField();
        insuranceLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        insurance = new javax.swing.JTextField();
        birthLabel = new javax.swing.JLabel();
        birth = new javax.swing.JTextField();
        ssnLabel = new javax.swing.JLabel();
        ssn = new javax.swing.JTextField();
        insuranceID = new javax.swing.JTextField();
        sexLabel = new javax.swing.JLabel();
        insuranceIDLabel = new javax.swing.JLabel();
        addPatientButtons = new javax.swing.JPanel();
        clearPatient = new javax.swing.JButton();
        submitPatient = new javax.swing.JButton();
        lastNameLabel = new javax.swing.JLabel();
        firstName = new javax.swing.JTextField();
        titleLabel = new javax.swing.JLabel();
        lastName = new javax.swing.JTextField();
        emailLabel = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        addressLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        address = new javax.swing.JTextArea();
        cityLabel = new javax.swing.JLabel();
        sex = new javax.swing.JComboBox();
        title = new javax.swing.JComboBox();
        emptyError = new javax.swing.JLabel();
        birthError = new javax.swing.JLabel();

        zipLabel.setText("Zip:");

        stateLabel.setText("State:");

        firstNameLabel.setText("First Name:");

        addDoctorHeader.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        addDoctorHeader.setText("Add New Doctor");

        insuranceLabel.setText("Insurance Company:");

        passwordLabel.setText("Password:");

        birthLabel.setText("Birth Date:");

        ssnLabel.setText("SSN:");

        sexLabel.setText("Sex:");

        insuranceIDLabel.setText("Insurance ID:");

        clearPatient.setText("Clear");
        clearPatient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearPatientActionPerformed(evt);
            }
        });
        addPatientButtons.add(clearPatient);

        submitPatient.setText("Submit");
        submitPatient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitPatientActionPerformed(evt);
            }
        });
        addPatientButtons.add(submitPatient);

        lastNameLabel.setText("Last Name:");

        titleLabel.setText("Title:");

        emailLabel.setText("Email Address:");

        addressLabel.setText("Address:");

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        address.setColumns(20);
        address.setRows(5);
        jScrollPane1.setViewportView(address);

        cityLabel.setText("City:");

        sex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--", "Male", "Female", "Other" }));

        title.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--", "Mr.", "Mrs.", "Ms.", "Miss", "Master" }));

        emptyError.setForeground(new java.awt.Color(255, 0, 0));
        emptyError.setText(" ");

        birthError.setForeground(new java.awt.Color(255, 0, 0));
        birthError.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addDoctorHeader)
                        .addGap(18, 18, 18)
                        .addComponent(emptyError))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lastNameLabel)
                            .addComponent(firstNameLabel)
                            .addComponent(addressLabel)
                            .addComponent(cityLabel)
                            .addComponent(zipLabel)
                            .addComponent(birthLabel)
                            .addComponent(insuranceLabel)
                            .addComponent(passwordLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(firstName, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                    .addComponent(lastName))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(titleLabel)
                                    .addComponent(sexLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(sex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(password, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(insurance, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                    .addComponent(birth, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(zip, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(city, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(stateLabel)
                                            .addComponent(ssnLabel)
                                            .addComponent(insuranceIDLabel)
                                            .addComponent(emailLabel))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(state)
                                            .addComponent(ssn)
                                            .addComponent(insuranceID, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                            .addComponent(email)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(birthError))))))
                    .addComponent(addPatientButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addDoctorHeader)
                    .addComponent(emptyError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNameLabel)
                    .addComponent(firstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(titleLabel)
                    .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameLabel)
                    .addComponent(lastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sexLabel)
                    .addComponent(sex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addressLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cityLabel)
                    .addComponent(stateLabel)
                    .addComponent(state, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zipLabel)
                    .addComponent(ssnLabel)
                    .addComponent(ssn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(birth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(birthLabel)
                    .addComponent(birthError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(insuranceLabel)
                    .addComponent(insurance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(insuranceIDLabel)
                    .addComponent(insuranceID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailLabel)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(addPatientButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void clearPatientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearPatientActionPerformed
        address.setText("");
        birth.setText("");
        city.setText("");
        firstName.setText("");
        lastName.setText("");
        ssn.setText("");
        state.setText("");
        zip.setText("");
        sex.setSelectedItem("--");
        title.setSelectedItem("--");
        password.setText("");
        email.setText("");
        insurance.setText("");
        insuranceID.setText("");
        emptyError.setText("");
        birthError.setText("");
    }//GEN-LAST:event_clearPatientActionPerformed

    private void submitPatientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitPatientActionPerformed
        String pass = new String(password.getPassword());
        String sexString = (String) sex.getSelectedItem();
        String titleString = (String) title.getSelectedItem();
        String username;
        int doctorCount = 0, patientCount = 0, nurseCount = 0, concatNum = 0;
        boolean notTaken = false;
        PreparedStatement statement;
        if (!address.getText().isEmpty() 
                && !birth.getText().isEmpty() 
                && !city.getText().isEmpty() 
                && !firstName.getText().isEmpty()
                && !lastName.getText().isEmpty()
                && !ssn.getText().isEmpty()
                && !state.getText().isEmpty()
                && !zip.getText().isEmpty()
                && !sexString.equals("--")
                && !titleString.equals("--")
                && !pass.equals("")
                && !email.getText().isEmpty()
                && !insurance.getText().isEmpty()
                && !insuranceID.getText().isEmpty()) {
            // Create Username
            username = (firstName.getText().substring(0, 1) + lastName.getText()).toLowerCase();
            while(!notTaken) {
                try {
                    ResultSet set1 = stat.executeQuery("SELECT COUNT(*) FROM doctors WHERE username = '" + username + "';");
                    doctorCount = set1.getInt(1);
                    ResultSet set2 = stat.executeQuery("SELECT COUNT(*) FROM nurses WHERE username = '" + username + "';");
                    patientCount = set2.getInt(1);
                    ResultSet set3 = stat.executeQuery("SELECT COUNT(*) FROM patients WHERE username = '" + username + "';");
                    nurseCount = set3.getInt(1);
                } catch (SQLException ex) {}
                if (doctorCount == 0 && patientCount == 0 && nurseCount == 0) {
                    notTaken = true;
                }
                else {
                    concatNum++;
                    username = (firstName.getText().substring(0, 1) + lastName.getText()).toLowerCase() + concatNum;
                }
            }
            try {
                // Parse Birthdate
                SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date date = sdfm.parse((String)birth.getText());
                java.sql.Date sqlDate = new Date(date.getTime());
                statement = conn.prepareStatement("INSERT INTO doctors "
                        + "(first, last, title, sex, address, city, state, zip, ssn, birthdate, insurance, insuranceID, username, password, email) "
                        + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
                statement.setString(1, firstName.getText());
                statement.setString(2, lastName.getText());
                statement.setString(3, titleString);
                statement.setString(4, sexString);
                statement.setString(5, address.getText());
                statement.setString(6, city.getText());
                statement.setString(7, state.getText());
                statement.setInt(8, Integer.parseInt(zip.getText()));
                statement.setInt(9, Integer.parseInt(ssn.getText()));
                statement.setDate(10, sqlDate);
                statement.setString(11, insurance.getText());
                statement.setString(12, insuranceID.getText());
                statement.setString(13, username);
                statement.setString(14, pass);
                statement.setString(15, email.getText());
                statement.executeUpdate();
                address.setText("");
                birth.setText("");
                city.setText("");
                firstName.setText("");
                lastName.setText("");
                ssn.setText("");
                state.setText("");
                zip.setText("");
                sex.setSelectedItem("--");
                title.setSelectedItem("--");
                password.setText("");
                email.setText("");
                insurance.setText("");
                insuranceID.setText("");
                emptyError.setText("Patient successfully added. Username is " + username + ".");
            } catch (SQLException ex) {System.out.println(ex.getMessage());}
            catch (ParseException ex) {birthError.setText("You must format your birthdate in this format: YYYY-MM-DD");}
        }
        else
            emptyError.setText("ERROR: You must fill in ALL the fields.");
    }//GEN-LAST:event_submitPatientActionPerformed
    // SQLite Variable declaration
    private Connection conn;
    private Statement stat;
    // End of SQLite Variable declaration
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addDoctorHeader;
    private javax.swing.JPanel addPatientButtons;
    private javax.swing.JTextArea address;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JTextField birth;
    private javax.swing.JLabel birthError;
    private javax.swing.JLabel birthLabel;
    private javax.swing.JTextField city;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JButton clearPatient;
    private javax.swing.JTextField email;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JLabel emptyError;
    private javax.swing.JTextField firstName;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField insurance;
    private javax.swing.JTextField insuranceID;
    private javax.swing.JLabel insuranceIDLabel;
    private javax.swing.JLabel insuranceLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField lastName;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JPasswordField password;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JComboBox sex;
    private javax.swing.JLabel sexLabel;
    private javax.swing.JTextField ssn;
    private javax.swing.JLabel ssnLabel;
    private javax.swing.JTextField state;
    private javax.swing.JLabel stateLabel;
    private javax.swing.JButton submitPatient;
    private javax.swing.JComboBox title;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JTextField zip;
    private javax.swing.JLabel zipLabel;
    // End of variables declaration//GEN-END:variables
}
